package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFTimeWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFTimeWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFTimeWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFTimeWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFTimeWidget widget) {
        ObjectNode ui = node.getUi();
        ui.put("widget", "time");
        putString(ui, "placeholder", widget.placeholder());
        putString(ui, "format", widget.format());
        putString(ui, "displayFormat", widget.displayFormat());
        ui.put("utcEpoch", widget.utcEpoch());
        ui.put("allowEmpty", widget.allowEmpty());
        putString(ui, "clearText", widget.clearText());
        ui.put("hideDisabledOptions", widget.hideDisabledOptions());
        ui.put("use12Hours", widget.use12Hours());
        putInt(ui, "hourStep", widget.hourStep());
        putInt(ui, "minuteStep", widget.minuteStep());
        putInt(ui, "secondStep", widget.secondStep());
        putString(ui, "popupClassName", widget.popupClassName());
        putString(ui, "nowText", widget.nowText());
        putString(ui, "okText", widget.okText());
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFTimeWidget.class};
    }
}
